<template>
  <page-layout v-if="!isLoading">
    <a-row :gutter="[10, 10]">
      <!-- 顶部区域 -->
      <a-col :span="24">
        <a-card>
          <div class="box-ver-hor-between">
            <div class="primary-color">学校管理</div>
            <div>
              <a-button type="primary" class="mr-20">批量导入</a-button>
              <a-button type="primary" v-if="btnIsPowerByCode('unversity:school:new')" @click="goNewSchool">
                新增学校
              </a-button>
            </div>
          </div>
        </a-card>
      </a-col>
      <!-- 中心区域 -->
      <a-col :span="24">
        <a-card>
          <div class="mb-20">
            <!-- 查询参数 -->
            <pro-query :searchParam="searchParams.params"
                       :labelCol="{ span: 10 }"
                       :wrapperCol="{ span: 14 }"
                       @on-search="search"
            />
          </div>
          <!-- 列表 -->
          <pro-table ref="tableRef"
                     :pagination="pagination"
                     :fetch="fetch"
                     :columns="tableConfig.columns"
                     :operate="tableConfig.operate"
                     :param="state.param"
                     :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }"
          />
        </a-card>
      </a-col>
    </a-row>
  </page-layout>
</template>

<script setup>
import { message, Modal } from 'ant-design-vue'
import { page, remove } from '@/api/module/school'
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { btnIsPowerByCode, filterEmptyByObject } from '@/tools/common'
import { getDictByCodesReturnObj } from '@/tools/get-dict'
import dictEnum from '@/const/dict'

const router = useRouter()

const pagination = {
  pageNum: 1,
  pageSize: 10,
}

const search = params => {
  state.param = {
    ...state.param,
    ...filterEmptyByObject(params)
  }
}

const tableConfig = reactive({
  columns: [],
  operate: [
    {
      label: "详情",
      event: item => router.push({
        path: '/university/college-manage/detail',
        query: { id: item.id }
      })
    },
    {
      label: "删除",
      event: item => {
        Modal.confirm({
          title: '你需要删除该学校数据吗？',
          okText: 'Yes',
          okType: 'danger',
          cancelText: 'No',
          onOk() {
            remove({ id: item.id }).then(() => {
              message.success({ content: '删除成功' })
            })
          },
        })
      }
    }
  ]
})

const isLoading = ref(false)

const state = reactive({
  selectedRowKeys: [],
  param: {},
  visibleSave: false,
  visibleEdit: false,
  visibleInfo: false,
  recordEdit: {},
  recordInfo: {},
})

const goNewSchool = () => router.push('/university/college-manage/add')

const fetch = async param => {
  const response = await page(param)
  return {
    data: response.data.record,
    total: response.data.total
  }
}

let dictLists = reactive({
  data: {}
})
let searchParams = reactive({
  params: []
})

onMounted(async () => {
  isLoading.value = true

  dictLists.data = await getDictByCodesReturnObj([
    dictEnum.YES_OR_NO,
    dictEnum.SCH_TYPE,
    dictEnum.SCH_SYSTEM,
    dictEnum.SCH_NATURE,
    dictEnum.FOLLOW_PROGRESS,
    dictEnum.Intention_PROJECT
  ])

  searchParams.params = [
    { key: "schResponsiblePerson", type: "input", label: "联系人姓名", placeholder: '请输入联系人姓名' },
    { key: "schResponsiblePerson", type: "input", label: "职务", placeholder: '请输入职务' },
    { key: "schoolCnName", type: "input", label: "学校名", placeholder: '请输入学校名' },
    { key: "schCategory", type: "select", label: "类别", placeholder: '请选择', options: dictLists.data[dictEnum.SCH_TYPE] },
    {
      key: "schSchoolSystem",
      type: "select",
      label: "学制",
      placeholder: '请选择',
      options: dictLists.data[dictEnum.SCH_SYSTEM]
    },
    {
      key: "schOnlineYesno",
      type: "select",
      label: "是否有网课",
      placeholder: '请选择',
      options: dictLists.data[dictEnum.YES_OR_NO]
    },
    {
      key: "schCompetitorYesno",
      type: "select",
      label: "是否与竞争对手合作",
      placeholder: '请选择',
      options: dictLists.data[dictEnum.YES_OR_NO]
    },
    {
      key: "schFollowUpProgress",
      type: "select",
      label: "跟进进度",
      placeholder: '请选择',
      options: dictLists.data[dictEnum.FOLLOW_PROGRESS]
    },
    {
      key: "schIntendedProject",
      type: "select",
      label: "意向项目",
      placeholder: '请选择',
      options: dictLists.data[dictEnum.Intention_PROJECT]
    },
  ]

  tableConfig.columns = [
    { dataIndex: "schoolCnName", key: "schoolCnName", title: "学校中文名" },
    { dataIndex: "schoolEnName", key: "schoolEnName", title: "学校英文名" },
    { dataIndex: "schCountry", key: "schCountry", title: "所属国家" },
    {
      dataIndex: "schNature",
      key: "schNature",
      title: "性质",
      cellRender: (record, data) => {
        if (dictLists.data[dictEnum.SCH_NATURE]) {
          const item = dictLists.data[dictEnum.SCH_NATURE].find(v => v.value == data)
          return item ? item.label : ''
        }
        return ''
      }
    },
    {
      dataIndex: "schCategory",
      key: "schCategory",
      title: "类别",
      cellRender: (record, data) => {
        const item = dictLists.data[dictEnum.SCH_TYPE].find(v => v.value == data)
        return item ? item.label : ''
      }
    },
    {
      dataIndex: "schSchoolSystem",
      key: "schSchoolSystem",
      title: "学制",
      cellRender: (record, data) => {
        const item = dictLists.data[dictEnum.SCH_SYSTEM].find(v => v.value == data)
        return item ? item.label : ''
      }
    },
    { dataIndex: "schFollowUpProgress", key: "schFollowUpProgress", title: "跟进进度",
      cellRender: (record, data) => {
              const item = dictLists.data[dictEnum.FOLLOW_PROGRESS].find(v => v.value == data)
              return item ? item.label : ''
            }
    },
    {
      dataIndex: "schOnlineYesno",
      key: "schOnlineYesno",
      title: "是否有网课",
      cellRender: (record, data) => {
        const item = dictLists.data[dictEnum.YES_OR_NO].find(v => v.value == data)
        return item ? item.label : ''
      }
    },
    {
      dataIndex: "schCompetitorYesno",
      key: "schCompetitorYesno",
      title: "是否与竞争对手有合作",
      cellRender: (record, data) => {
        const item = dictLists.data[dictEnum.YES_OR_NO].find(v => v.value == data)
        return item ? item.label : ''
      }
    },
    { dataIndex: "schResponsiblePerson", key: "schResponsiblePerson", title: "负责人" },
    { dataIndex: "createTime", key: "createTime", title: "创建时间" },
  ]

  isLoading.value = false
})
</script>
